﻿using System;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
using Common;

namespace RichECMS.ServerPages.CoderEditor
{
    public partial class FileTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (State.LoginUser.ManagerCompetence > (int)Utility.ManagerCompetence.Designer)
            {
                Response.Write("您没有权限查看改页面");
                Response.End();
            }

            if (!Page.IsPostBack)
            {
                BuildTree();
            }
        }

        public string BuildTree()
        {
            List<Hashtable> result = new List<Hashtable>();

            if (State.LoginUser.TreeHash["IssuePath"] == null)
            {
                State.LoginUser.TreeHash["IssuePath"] = State.LoginUser.IssuePath;
            }
            Hashtable hashIssue = new Hashtable();
            hashIssue = FildChildFileAndDorectory(State.LoginUser.TreeHash["IssuePath"].ToString(), State.LoginUser.Rootpath);
            hashIssue["name"] = "资源文件";
            hashIssue["open"] = true;
            result.Add(hashIssue);

            if (State.LoginUser.TreeHash["TemplateRoot"] == null)
            {
                State.LoginUser.TreeHash["TemplateRoot"] = State.LoginUser.TemplatePath;
            }
            Hashtable hashTemplate = new Hashtable();
            hashTemplate = FildChildFileAndDorectory(State.LoginUser.TreeHash["TemplateRoot"].ToString(), State.LoginUser.Rootpath );
            hashTemplate["name"] = "模板文件";
            result.Add(hashTemplate);

            return JsonMapper.ToJson(result);
        }

        private Hashtable FildChildFileAndDorectory(string currentPath, string repleasePath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(currentPath);
                Hashtable hashTemp = new Hashtable();
                hashTemp["name"] = dir.Name;
                hashTemp["open"] = false;
                hashTemp["creattime"] = dir.CreationTime;
                hashTemp["lastwritetime"] = dir.LastWriteTime;
                hashTemp["lifetype"] = "directory";
                hashTemp["isParent"] = true;
                hashTemp["iconSkin"] = "directory";
                if (dir.GetDirectories().Length + dir.GetFiles().Length > 0)
                {
                    List<Hashtable> hashList = new List<Hashtable>();
                    if (dir.GetDirectories().Length != 0)
                    {
                        foreach (DirectoryInfo dirtemp in dir.GetDirectories())
                        {
                            if (dirtemp.Name != "ahtml" && dirtemp.Name != "shtml" && dirtemp.Name != "Pcfiles")
                            {
                                if (dirtemp.GetDirectories().Length + dirtemp.GetFiles().Length > 0)
                                {
                                    hashList.Add(FildChildFileAndDorectory(dirtemp.FullName, repleasePath));
                                }
                            }
                        }
                    }
                    if (dir.GetFiles().Length != 0)
                    {
                        foreach (FileInfo filetemp in dir.GetFiles())
                        {
                            Hashtable temp = new Hashtable();
                            temp["name"] = filetemp.Name;
                            temp["size"] = filetemp.Length > 1024 ? (filetemp.Length / 1024 + " kb") : filetemp.Length + " byte";
                            temp["creattime"] = filetemp.CreationTime;
                            temp["lastwritetime"] = filetemp.LastWriteTime;
                            temp["lifetype"] = "file";
                            temp["iconSkin"] = filetemp.Extension.ToLower().Replace(".", "");
                            temp["filepath"] = filetemp.FullName.Replace(repleasePath, "").Replace("\\", "/");
                            temp["filetype"] = filetemp.Extension.ToLower().Replace(".", "");
                            hashList.Add(temp);
                        }
                    }
                    hashTemp["children"] = hashList;
                }
                return hashTemp;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}